﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

using MyToDoList.Model;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections.Specialized;

namespace MyToDoList.ViewModel
{
    public class TaskViewModel 
    {
        public event PropertyChangedEventHandler PropertyChanged;
        //public ObservableCollection<TaskViewModel> ItemsObserve { get; set; }
        public ObservableCollection<Task> Items { get; set; }
        
        public TaskViewModel()
        {
            this.Items = new ObservableCollection<Task>();
            IsDataLoaded = false;
        }


        /*private void  Items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            switch(e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                {
                    
                    IsDataLoaded = true;
                }
                break;
                case NotifyCollectionChangedAction.Remove:
                {
                    
                    IsDataLoaded = true;
                }
                break;
                case NotifyCollectionChangedAction.Reset:
                {
                }
                break;

            }
        }*/
        
        public void Update(Task t)
        {
            int index = this.Items.IndexOf(t);
            this.Items[index] = t;
            
        }


        public void StoreData()
        {
            Task.Save(Items);
        }

        public void LoadData()
        {
            this.Items.Clear();
            var tasks = Task.LoadAll();
            if (tasks != null)
            {
                foreach (var t in tasks)
                    this.Items.Add(t);
                this.IsDataLoaded = true;
            }
        }




        public bool IsDataLoaded { get; set; }


        public event NotifyCollectionChangedEventHandler CollectionChanged;
    }
}
